package com.jobpass.controller;


import com.alibaba.excel.EasyExcel;
import com.jobpass.common.data.ResultData;
import com.jobpass.common.model.Student;
import com.jobpass.common.service.StudentService;
import com.jobpass.listen.StudentListener;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@Slf4j
@Api(tags = "处理教师信息")
@RestController
@RequestMapping("/teacher")
public class TeacherController {


    @Resource
    private StudentService studentServiceImpl;

    /**
     * @author Beloved
     * 教师端从Excel批量导入学生信息到数据库
     */
    @ApiOperation(value = "批量导入学生信息", notes = "必须有学生信息")
    @PostMapping("/uploadExcel")
    public ResultData uploadExcel(@RequestParam(value = "file") MultipartFile file){

        try {
            EasyExcel.read(file.getInputStream(), Student.class , new StudentListener(studentServiceImpl)).sheet().doRead();
            return ResultData.success();
        } catch (IOException e) {
            e.printStackTrace();
            log.debug("批量导入学生信息失败！异常信息："+ e);
            return ResultData.fail();
        }
    }

}
